package com.raspositos.guitarfrik.android;

import java.util.Iterator;
import java.util.SortedSet;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.MediaStore.MediaColumns;
import android.provider.MediaStore.Audio.Albums;
import android.provider.MediaStore.Audio.AudioColumns;
import android.util.Log;

public class TrackDescriptor {
	int id;
	String name;
	String title;
	int duration;
	String artist;
	String album;
	String album_art;
    int album_id;
    String location;
    Context ctxt;
    private static TrackSectionDAO dao;
    SortedSet<TrackSection> section;
    
    public final String getLocation(){
    	return location;
    }
    
	public final int getId() {
		return id;
	}
	public final String getName() {
		return name;
	}
	public final String getTitle() {
		return title;
	}
	public final int getDuration() {
		return duration;
	}
	public final String getArtist() {
		return artist;
	}
	public final String getAlbum() {
		return album;
	}
	public final Drawable getAlbumArt(Activity ctc) {
		if (album_art == null){
			fetchAlbumArt(ctc);
		}
		if ("".equals(album_art) || album_art == null){
			return ctxt.getResources().getDrawable(R.drawable.notes_sh_icon);
		} else {
			return new BitmapDrawable(ctxt.getResources(), BitmapFactory.decodeFile(album_art));
		}
	}	
	private  void fetchAlbumArt(Activity ctc) {
//		Log.d("GuitarFrik","fecthAlbumArt(i):" + album_art);
		Cursor xcur = null;
		try{
			xcur = ctc.managedQuery(Albums.EXTERNAL_CONTENT_URI, new String[]{AudioColumns._ID, 
					AudioColumns.ALBUM_ART}, 
					AudioColumns._ID + " = "+ this.album_id, new String[]{}, "");
	
			if (xcur == null || xcur.getCount() < 1 ){
				album_art = "";
			} else {
				xcur.moveToFirst();
				album_art = xcur.getString(xcur.getColumnIndex(AudioColumns.ALBUM_ART));
				if (album_art == null){
					album_art = "";
				}
			}
	} catch (SQLException e){
		Log.e("GuitarFrik", "Unexpected error while fetching album art.", e);
	} finally{
		if (xcur != null)
			xcur.close();
	}
//	Log.d("GuitarFrik","fecthAlbumArt(o):" + album_art);
	
	}
	
	public final int getAlbum_id() {
		return album_id;
	}
	
	private TrackDescriptor(){}
	
	public static final TrackDescriptor newTrackDescriptor(Cursor cur, Activity ctc){
		TrackDescriptor td = new TrackDescriptor();
		td.ctxt = ctc;
		td.id = cur.getInt(cur.getColumnIndex(MediaColumns._ID));
		td.title = cur.getString(cur.getColumnIndex(MediaColumns.TITLE));
		td.album = cur.getString(cur.getColumnIndex(AudioColumns.ALBUM));
		td.album_id = cur.getInt(cur.getColumnIndex(AudioColumns.ALBUM_ID));
		td.artist = cur.getString(cur.getColumnIndex(AudioColumns.ARTIST));
		td.duration = cur.getInt(cur.getColumnIndex(AudioColumns.DURATION));
		td.name = cur.getString(cur.getColumnIndex(MediaColumns.DISPLAY_NAME));
		td.location = cur.getString(cur.getColumnIndex(MediaColumns.DATA));
//		Cursor xcur = fetchAlbumArt(ctc, td);
		if (dao == null){
			dao = new TrackSectionDAO(ctc);
		}
		return td;
	}



	/**
	 * Adds or replaces Tracks Section
	 * @param trackSection
	 */
	public void addSection(TrackSection trackSection) {
		if (section == null ){
			section = dao.loadAll(this);
		}
		for (Iterator<TrackSection> it = section.iterator(); it.hasNext();) {
			TrackSection next = it.next();
			if (next.getName().equals(trackSection.getName())){
				trackSection.setId(next.getId());
				dao.update(trackSection);
				section.remove(next);
				section.add(trackSection);
				return;
			}
		}
		section.add(trackSection);
		trackSection.setId(dao.insert(trackSection));
	}
	
	public TrackSection[] getSections(){
		if (section == null ){
//			Log.d("GuitarFrik","getSections");
			section = dao.loadAll(this);
		}
		return section.toArray(new TrackSection[]{});
	}

	public void removeSection(TrackSection trackSection) {
		dao.delete(trackSection);
		section.remove(trackSection);
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result
				+ ((location == null) ? 0 : location.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TrackDescriptor other = (TrackDescriptor) obj;
		if (id != other.id)
			return false;
		if (location == null) {
			if (other.location != null)
				return false;
		} else if (!location.equals(other.location))
			return false;
		return true;
	}

	
	
	
}
